package com.generic.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.generic.dao.ModuleDao;
import com.generic.ds.HibernateSession;
import com.generic.module.impl.Member;
import com.generic.util.Constant;

public class MemberDao extends ModuleDao {
	
	public MemberDao(HibernateSession session) {
		super(session, Member.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> list() {
		StringBuffer sb = new StringBuffer("select");
		sb.append(" m.id, m.First_Name, m.Last_Name, m.Gender");
		sb.append(", concat('/").append(Constant.IMAGE_THUMB_PATH).append("',cast(i.Profile_Id as char),'/',i.File_Name) as Image_Path");
		sb.append(" from Member m");
		sb.append(" left join Image i");
		sb.append(" on i.Id = m.Image_Id");
		sb.append(" where m.status = 'A'");
		
		Query query = dsSession.createSQLQuery(sb.toString());
		
		return query.list();
	}
	
	public int setDefaultImage(Long memberId, Long imageId) {
		StringBuffer sb = new StringBuffer("update Member set");
		sb.append(" Image_Id = :imageId");
		sb.append(" where id = :memberId");
		
		Query query = dsSession.createSQLQuery(sb.toString());
		query.setLong("imageId", imageId);
		query.setLong("memberId", memberId);
		
		return query.executeUpdate();
	}
}
